package com.cyyz.util;

import com.cyyz.entity.BlogAndTag;
import com.cyyz.entity.Tag;
import sun.rmi.runtime.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName StringInterconversionList
 * @Description TODO
 * @Author 简笔轩客
 * @Date 2020/6/1 17:07
 * @Version 1.0
 */
public class StringInterconversionList {

    //1. String转成List,T为list内的值
    public static Map<Integer,List> stringToList (String string) {
        //存放已有的标签
        List list1 = new ArrayList<>();
        //存放新加的标签
        List list2 = new ArrayList<>();
        Map<Integer,List> map = new HashMap<>();
        if (!"".equals(string) && string != null) {
            String[] idarray = string.split(",");
            for (int i = 0; i < idarray.length; i++) {
                StringBuffer number = new StringBuffer();
                StringBuffer characters = new StringBuffer();
                char[] chars = idarray[i].toCharArray();
                for (int j = 0; j < chars.length; j++) {
                    if (!(chars[j]+"").matches("[0-9]")){
                        list2.add(idarray[i]);
                        break;
                    }
                    if (j+1 == chars.length)
                        list1.add(idarray[i]);
                }
            }
        }
        map.put(0,list1);
        map.put(1,list2);
        return map;
    }
    //2. List转成String
    public static String listToString (List<Tag> list) {
        if(!list.isEmpty()){
            StringBuffer ids = new StringBuffer();
            boolean flag = false;
            for(Tag tag: list){
                if(flag){
                    ids.append(",");
                }else {
                    flag = true;
                }
                ids.append(tag.getId());
            }
            return ids.toString();
        }else {
            return null;
        }
    }
}
